﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BOSS.Framework;
using BOSS.Framework.Search.Context;
using BOSS.Domain.Security;
using BOSS.Service.Warehouses;
using BOSS.Domain.Warehouses;

namespace BOSS.Web.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class WarehouseController : AbstractController
    {
        /// <summary>
        /// 获取 / 设置 仓库服务
        /// </summary>
        public IWarehouseService WarehouseService { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Create(int? id)
        {
            Warehouse warehouse = new Warehouse();

            if (id != null)
            {
                warehouse = this.WarehouseService.FindById(id.Value);
            }

            if (this.Request.IsPost())
            {
                TryUpdateModel(warehouse, new[]{"Name", "Address"});
                if (ModelState.IsValid)
                {
                    this.WarehouseService.SaveOrUpdate(warehouse);
                    return View("Details", warehouse);
                }
            }

            return View(warehouse);
        }

        /// <summary>
        /// 仓库详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                throw new LogicalException("仓库 Id 为空.");
            }

            Warehouse warehouse = this.WarehouseService.FindById(id.Value);
            if (warehouse == null)
            {
                throw new LogicalException(string.Format("未能找到 Id 为 {0} 的仓库.", id));   
            }

            return View(warehouse);
        }
    }
}
